﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace HRMService
{ // NOTE: If you change the class name "Science" here, you must also update the reference to "Science" in App.config.
    public partial class MainService : IScience
    {
        public DataSet Science_GetAll()
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Science_GetAll", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet Science_GetList(out int ItemCount, int currentPage, int pageSize, int branchID, int departmentID, int month, int year)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Science_GetList", dbConn);
            dbCmd.Parameters.AddWithValue("@CurrentPage", currentPage);
            dbCmd.Parameters.AddWithValue("@PageSize", pageSize);
            dbCmd.Parameters.AddWithValue("@BranchID", branchID);
            dbCmd.Parameters.AddWithValue("@DepartmentID", departmentID);
            dbCmd.Parameters.AddWithValue("@month", month);
            dbCmd.Parameters.AddWithValue("@year", year);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
                ItemCount = Convert.ToInt32(retVal.Tables[1].Rows[0][0]);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public ScienceInfo Science_GetInfo(int ScienceID)
        {
            ScienceInfo retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Science_GetInfo", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Science_ID", ScienceID);
            SqlDataReader dr = null;
            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new ScienceInfo();
                    retVal.ID = Convert.ToInt32(dr["Science_ID"]);
                    retVal.BranchID = Convert.ToInt32(dr["Science_BranchID"]);
                    retVal.DepartmentID = Convert.ToInt32(dr["Science_DepartmentID"]);
                    retVal.EmployeeID = Convert.ToInt32(dr["Science_EmployeeID"]);
                    retVal.FromDate = Convert.ToDateTime(dr["Science_FromDate"]);
                    retVal.ToDate = Convert.ToDateTime(dr["Science_ToDate"]);
                    retVal.ClassManagement = dr["Science_ClassManagement"].ToString();
                    retVal.ClassOwner = dr["Science_ClassOwner"].ToString();
                    retVal.Name = dr["Science_Name"].ToString();
                    retVal.Tenor = dr["Science_Tenor"].ToString();
                    retVal.Result = dr["Science_Result"].ToString();
                    retVal.TakePartAsName = dr["Science_TakePartAsName"].ToString();
                    retVal.ResultDate = dr["Science_ResultDate"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(dr["Science_ResultDate"]);
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }

        public int Science_Insert(ScienceInfo eInfo)
        {
            int retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Science_Insert", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Science_BranchID", eInfo.BranchID);
            dbCmd.Parameters.AddWithValue("@Science_DepartmentID", eInfo.DepartmentID);
            dbCmd.Parameters.AddWithValue("@Science_EmployeeID", eInfo.EmployeeID);
            dbCmd.Parameters.AddWithValue("@Science_Name", eInfo.Name);
            dbCmd.Parameters.AddWithValue("@Science_Tenor", eInfo.Tenor);
            dbCmd.Parameters.AddWithValue("@Science_Result", eInfo.Result);
            dbCmd.Parameters.AddWithValue("@Science_FromDate", eInfo.FromDate);
            dbCmd.Parameters.AddWithValue("@Science_ToDate", eInfo.ToDate);
            dbCmd.Parameters.AddWithValue("@Science_TakePartASName", eInfo.TakePartAsName);
            dbCmd.Parameters.AddWithValue("@Science_ClassManagement", eInfo.ClassManagement);
            dbCmd.Parameters.AddWithValue("@Science_ClassOwner", eInfo.ClassOwner);
            if (eInfo.ResultDate == DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@Science_ResultDate", DBNull.Value);
            else
                dbCmd.Parameters.AddWithValue("@Science_ResultDate", eInfo.ResultDate);
            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public bool Science_Update(ScienceInfo eInfo)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Science_Update", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Science_ID", eInfo.ID);
            dbCmd.Parameters.AddWithValue("@Science_BranchID", eInfo.BranchID);
            dbCmd.Parameters.AddWithValue("@Science_DepartmentID", eInfo.DepartmentID);
            dbCmd.Parameters.AddWithValue("@Science_EmployeeID", eInfo.EmployeeID);
            dbCmd.Parameters.AddWithValue("@Science_Name", eInfo.Name);
            dbCmd.Parameters.AddWithValue("@Science_Tenor", eInfo.Tenor);
            dbCmd.Parameters.AddWithValue("@Science_Result", eInfo.Result);
            dbCmd.Parameters.AddWithValue("@Science_FromDate", eInfo.FromDate);
            dbCmd.Parameters.AddWithValue("@Science_ToDate", eInfo.ToDate);
            dbCmd.Parameters.AddWithValue("@Science_TakePartASName", eInfo.TakePartAsName);
            dbCmd.Parameters.AddWithValue("@Science_ClassManagement", eInfo.ClassManagement);
            dbCmd.Parameters.AddWithValue("@Science_ClassOwner", eInfo.ClassOwner);
            if (eInfo.ResultDate == DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@Science_ResultDate", DBNull.Value);
            else
                dbCmd.Parameters.AddWithValue("@Science_ResultDate", eInfo.ResultDate);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public bool Science_Delete(int ScienceID)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Science_Delete", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Science_ID", ScienceID);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
    }
}
